<template>
  <a-form :rules="noticeRules" :model="noticeFormConfig.form" @finish="submit" :label-col="{ span: 6 }"
    :wrapper-col="{ offset: 1 }" label-align="right" ref="noticeFormRef" :disabled="hasPerm('system:notice:edit')">
    <FormCard title="编辑/新增" :back="back" subtitle="通知公告">
      <template #extra>
        <a-button htmlType="submit" type="primary" :loading="noticeTable.loading" block>
          保存
        </a-button>
      </template>
      <a-row :gutter="8">
        <a-col :span="16">
          <a-card>
            <a-form-item label="公告标题" name="noticeTitle">
              <a-input allow-clear placeholder="请输入公告标题" v-model:value="noticeFormConfig.form.noticeTitle">
              </a-input>
            </a-form-item>
            <a-form-item label="公告类型" name="noticeType">
              <a-input allow-clear placeholder="请输入公告类型（1通知 2公告）" v-model:value="noticeFormConfig.form.noticeType">
              </a-input>
            </a-form-item>
            <a-form-item label="公告内容" name="noticeContent">
              <a-textarea allow-clear placeholder="请输入公告内容"
                v-model:value="noticeFormConfig.form.noticeContent"></a-textarea>
            </a-form-item>
            <a-form-item label="公告状态" name="status">
              <a-input allow-clear placeholder="请输入公告状态（0正常 1关闭）" v-model:value="noticeFormConfig.form.status">
              </a-input>
            </a-form-item>
            <a-form-item label="备注" name="remark">
              <a-input allow-clear placeholder="请输入备注" v-model:value="noticeFormConfig.form.remark">
              </a-input>
            </a-form-item>
          </a-card>
        </a-col>
        <AxExtra></AxExtra>
      </a-row>
    </FormCard>
  </a-form>
</template>

<script setup lang="ts">
import {
} from '../data/options';
import { hasPerm } from '@/utils/common/perm';
import type { Response } from '@/api/config/types';
import {
  createNotice,
  updateNotice,
} from '@/api/modules/system/notice/notice';
import { notify, } from '@/utils/table/table';
import { noticeList } from '../data/curd';
import {
  noticeFormConfig,
  noticeRules,
  noticeFormRef
} from '../data/form';
import { noticeTable } from '../data/table';
import AxExtra from './AxExtra.vue';

const submit = async () => {
  noticeTable.value.loading = true;
  let result: undefined | Response<any> = undefined
  if (noticeFormConfig.value.form.noticeId) {
    const { data } = await updateNotice(noticeFormConfig.value.form)
    result = data
  } else {
    const { data } = await createNotice(noticeFormConfig.value.form)
    result = data
  }
  notify(result)
  noticeTable.value.loading = false;
  await noticeList();
  back();

};

const back = () => {
  noticeFormConfig.value.display = false;
  noticeFormConfig.value.reset!();
};

</script>

<style lang="scss" scoped></style>
